# -*- coding: utf-8 -*-
from odoo import models, fields, api


class ProductionOperation(models.Model):
    """生产工序"""
    _name = 'production.operation'
    _description = '生产工序'
    _order = 'sequence, code'
    
    code = fields.Char(
        string='工序编号',
        required=True,
        help='工序编号'
    )
    
    name = fields.Char(
        string='工序名称',
        required=True,
        help='工序名称'
    )
    
    sequence = fields.Integer(
        string='序号',
        default=10,
        help='工序顺序'
    )
    
    workcenter_id = fields.Many2one(
        'production.workcenter',
        string='工作中心',
        help='执行此工序的工作中心'
    )
    
    description = fields.Text(
        string='描述',
        help='工序描述'
    )
    
    active = fields.Boolean(
        string='激活',
        default=True
    )
    
    _sql_constraints = [
        ('code_unique', 'UNIQUE(code)', '工序编号必须唯一！')
    ]
    
    def name_get(self):
        result = []
        for record in self:
            name = f"[{record.code}] {record.name}"
            result.append((record.id, name))
        return result

